Let us establish some context on what we are doing in this notebook.
We will save a Tensor to CSV.
In [1]:
require 'csvigo';
In [2]:
x = torch.randn(5)
In [3]:
csvigo.save{path='test.csv', data={x:storage():totable()}, mode='raw', header=false}
Out[3]:
In [4]:
os.execute('cat test.csv');
Out[4]:
In [5]:
x = {}
for i=1,3 do table.insert(x, torch.randn(5)) end -- 3 rows
In [6]:
y = {}
for i=1,3 do table.insert(y, x[i]:totable()) end
csvigo.save{path='test.csv', data=y, mode='raw', header=false}
Out[6]:
In [7]:
os.execute('cat test.csv');
Out[7]:
In [8]:
x = torch.randn(3,5)
In [9]:
csvigo.save{path='test.csv', data=x:totable(), mode='raw', header=false}
Out[9]:
In [10]:
os.execute('cat test.csv');
Out[10]:
If you think about a 3D tensor, there is no way to write it as CSV, CSV files have rows and columns, they do not have a third dimension.
However, let's say you have a 3D tensor, but want to write each 2D slice after the other, i.e.
if you have a tensor t which is of size 3x2x2, then you want to write t[1], then t[2] and then t[3].
This can be simply done using a tensor view, and writing as a 2D tensor, i.e.:
In [11]:
x = torch.randn(3,2,2)
In [12]:
y=x:view(3*2,2)
In [13]:
csvigo.save{path='test.csv', data=y:totable(), mode='raw', header=false}
Out[13]:
In [14]:
os.execute('cat test.csv');
Out[14]:
In [ ]: